package interfaces;

/**
 * @classname:LucyDarw
 * @description:
 * @author: Mr.Z
 * @date: 2020/11/28 17:38
 * @version: 0.1
 * @since:1.8
 */

import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;


/**
 * @packgename:Day03
 * @classname:LuckyDraw
 * @description:幸运抽奖
 * @author: JAVASM
 * @date: 2020/11/25 22:47
 * @version: 1.0
 * @since:1.8
 */
public class LuckyDraw {
    Scanner sc = new Scanner(System.in);
    HashMap<String, User> user = new HashMap();
    boolean userlogin = false;
    Random random = new Random();

    public void MainMenu() {
        int caozuo;
        System.out.printf("      ********欢迎使用幸运抽奖系统********    \n");

        System.out.printf("             1.注册\n");

        System.out.printf("             2.登陆\n");

        System.out.printf("             3.抽奖\n");

        System.out.printf("             4.退出登陆\n");

        System.out.printf("What Can I Do For You?");
        caozuo = sc.nextInt();
        switch (caozuo) {
            case 1:
                zhuce();
                break;
            case 2:
                Login();
                break;
            case 3:
                choujiang();
                break;
            case 4:
                excit();
            default:
                System.out.println("无效输入");

        }

    }

    public void Login() {
        String temp;
        String name;
        String password;
        System.out.println("  [幸运抽奖系统<登陆]");
        System.out.println();
        System.out.printf("还要继续吗？（y/n）:");
        temp = sc.next();
        if (temp.equalsIgnoreCase("n")) {
            MainMenu();
        }

        if (userlogin == true) {
            System.out.println(" ******您已登录******\n");
            System.out.println("********无需再次登陆\n");
            MainMenu();
        }
        System.out.println();
        sc.nextLine();
        System.out.printf("    ********* 请输入用户名：\n");
        name = sc.nextLine();
        if (user.containsKey(name) == false) {
            System.out.println(" ******查无此人，请先去注册！！！！\n");
            MainMenu();
        }
        System.out.println();
        System.out.printf("******** 请输入密码：\n");
        password = sc.nextLine();
        if (password.equals(user.get(name).userpassword)) {
            userlogin = true;
            System.out.printf("欢迎您%s\n", name);
            System.out.println("***恭喜您登陆成功*****\n");
            MainMenu();
        }
        System.out.println("******密码错误 请重新输入\n");
        Login();

    }

    public void excit() {
        String temp;
        System.out.println("  [幸运抽奖系统<退出登陆]\n");
        System.out.println();
        System.out.printf("还要继续吗？（y/n）:\n");
        temp = sc.next();
        if (temp.equalsIgnoreCase("n")) {
            MainMenu();
        }
        if (userlogin == false) {
            System.out.println("     您还未登录请先去登录\n");
            MainMenu();
        }
        userlogin = false;
        System.out.println("   退出登录成功！\n");

    }

    public void zhuce() {
        String temp;
        System.out.println("  [幸运抽奖系统<注册]");
        System.out.println();
        System.out.printf("还要继续吗？（y/n）:\n");
        temp = sc.next();
        if (temp.equalsIgnoreCase("n")) {
            MainMenu();
        }
        if (temp.equalsIgnoreCase("y")) {
            System.out.printf("********欢迎开始注册*******\n");

            sc.nextLine();
            System.out.printf("请输入用户名：\n");
            String name = sc.nextLine();
            if (user.containsKey(name)) {
                System.out.println("用户名重复\n");
                zhuce();
            }
            System.out.println();
            System.out.printf("请输入密码：\n");
            String password = sc.nextLine();
            String Id = String.valueOf(random.nextInt(8999) + 1000);
            User user1 = new User(Id, password);
            user.put(name, user1);
            System.out.println("注册成功\n");
            System.out.printf(" 请记好账号信息!\t用户名：%s\n密码：%s 卡号：%s\t", name, password, Id);
            MainMenu();
        }

    }

    public void choujiang() {
        boolean luckman = false;
        String temp;
        String[] lucknum = new String[5];
        String userid;
        System.out.println("  [幸运抽奖系统<幸运大抽奖]");
        System.out.println();
        System.out.printf("还要继续吗？（y/n）:\n");
        temp = sc.next();
        if (temp.equalsIgnoreCase("n")) {
            MainMenu();
        }
        if (userlogin == false) {
            System.out.printf("您还未登录，请先去登录吧\n");
            MainMenu();
        }
        for (int i = 0; i < lucknum.length; i++) {
            lucknum[i] = String.valueOf(random.nextInt(8999) + 1000);
        }
        sc.nextLine();
        System.out.println("请输入您的会员卡号：\n");
        userid = sc.nextLine();
        for (String s : lucknum) {
            if (s.equals(userid)) {
                System.out.printf("恭喜您,您是今天的幸运会员呦！！！！\n");
                luckman = true;
            }
            System.out.printf("本期幸运号码为：%s\n", s);
        }
        if (luckman == true) {
            choujiang();
        }
        System.out.printf("很抱歉您未能中奖！\n");
        choujiang();
    }

    public static void main(String[] args) {
        LuckyDraw luckyDraw = new LuckyDraw();
        luckyDraw.MainMenu();
    }

    class User {
        String userId;
        String userpassword;

        User(String Id, String password) {
            userId = Id;
            userpassword = password;
        }
    }
}
